from django.shortcuts import render
from authapp.models import UserProFile
# 导入user模型
from django.contrib.auth.models import User,Group
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
# 导入认证信息类
from django.contrib.auth import authenticate, login, logout


# Create your views here.
def goindex(request):
    return render(request, 'index.html')


# 创建测试用户
def createUser(request):
    User.objects.create_user(username='zs', password=123)
    print('测试用户创建成功')
    return HttpResponse('测试用户创建成功')


# 跳转信息页
@login_required
def detalis(request):
    return render(request, 'detalis.html')


# 定义登录函数
def mylogin(request):
    if request.POST:
        # 接收请求
        username = request.POST.get('user')
        userpass = request.POST.get('pass')
        # 验证用户
        user = authenticate(username=username, password=userpass)
        # 判断用户对象
        if user is not None:
            # 添加session
            login(request, user)
            # 跳转信息页
            return HttpResponseRedirect('/detalis/')
        else:
            return render(request, '/login.html/')
    else:
        return render(request, 'login.html')


# 退出登录业务
def mylogout(request):
    # 退出并清除session
    logout(request)
    return HttpResponseRedirect('/detalis/')


# 添加用户
def addUser(request):
    # 创建用户对象
    user = User()
    # 设置属性
    user.username = 'lisi'
    # 加密密码
    user.set_password('123')
    # 保存信息
    user.save()
    return HttpResponse('用户创建成功')


# 添加用户信息业务
def createUserMess(request):
    if request.POST:
        # 接收数据
        username = request.POST.get('user')
        userpass = request.POST.get('pass')
        phone = request.POST.get('phone')
        # 创建User对象和UserProFile
        user = User()
        # 设置账号密码
        user.username = username
        user.set_password(userpass)
        user.save()

        # 设置phone
        up = UserProFile()
        up.user_id = user.id
        up.phone = phone
        up.save()
        return render(request, 'addUser.html', {'msg': '用户创建成功'})
    else:
        return render(request, 'addUser.html')


# 修改密码
def updatapass(request,id):
    if request.POST:
        user=User.objects.get(id=id)
        passwd = request.POST.get('newpass')

        user.set_password(passwd)
        user.save()
        return HttpResponseRedirect('/detalis/')
    else:
        # return HttpResponseRedirect('/updatapass/')
        return render(request,'updatapass.html')



#创建组 group
def addGroup(request):
    if request.POST:
        #接收数据
        gname=request.POST.get('gname')
        #检测组名是否重复
        groupList=Group.objects.filter(name=gname)
        #判断查询结果中是否有数据
        if len(groupList)>0:
            return render(request,'addGroup.html',{'error':'组名不能重复'})
        #建立组
        Group.objects.create(name=gname)
        #响应客户端
        return render(request,'addGroup.html',{'msg':'组创建成功！'})
    else:
        return render(request,'addGroup.html')



#查询组
def queryAllGroups(request):
    #查询
    listGroup=Group.objects.all()
    content={}
    content['listGroup']=listGroup
    return render(request,'showGroup.html',content)


def addUserGroup(request,groupID):
    if request.POST:
        #接受参数
        UserIDs=request.POST.getlist('UserID')
        #获取组对象
        group=Group.objects.get(id=groupID)
        #遍历组获取对象
        for uid in UserIDs:
            #通过id获取对象
            user=User.objects.get(id=uid)
            #将对象加入组
            group.user_set.add(user)
        return HttpResponseRedirect('/queryAllGroups/')
    else:
        #根据id获取组对象
        group=Group.objects.get(id=groupID)
        #获取所有用户
        listUser=User.objects.all()
        #获取当前组中成员
        ownUsers = group.user_set.all()
        #建立字典数据
        content = {}
        content['group'] = group
        content['listUser'] = listUser.difference(ownUsers)
        return render(request,'addUserGroup.html',content)



#移除成员业务
def removeUser(request,groupID):
    if request.POST:
        group=Group.objects.get(id=groupID)
        #接收数据
        uidlist=request.POST.getlist('UserID')
        for uid in uidlist:
            user=User.objects.get(id=uid)
            #移除成员
            group.user_set.remove(user)
        return HttpResponseRedirect('/queryAllGroups/')
    else:
        #通过id获取组对象
        group=Group.objects.get(id=groupID)
        #获取组中所有成员
        listGroupUser = group.user_set.all()
        #建立字典数据
        content = {}
        content['group'] = group
        content['listGroupUser'] = listGroupUser
        return render(request,'removeUser.html',content)